package com.example.weli.ble2test;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class Slider extends View {
    private Drawable mBackground;
    private OnTouchListener mClickListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent m) {
            double position;
            Rect r = new Rect();
            Slider.this.getDrawingRect(r);
            if (Slider.this.mVertical) {
                position = Math.max(0.0d, (((double) r.bottom) - ((double) m.getY())) / ((double) r.height()));
            } else {
                position = Math.max(0.0d, (((double) m.getX()) - ((double) r.left)) / ((double) r.width()));
            }
            Slider.this.setPosition(Math.min(1.0d, position));
            return true;
        }
    };
    private Drawable mIndicator;
    private SliderPositionListener mListener;
    private double mPosition;
    private boolean mVertical;
    private double positionmax = 255.0d;

    interface SliderPositionListener {
        void disPosition(double d);

        void onPositionChange(double d);
    }

    public Slider(Context context) {
        super(context);
        initSliderView(context, false);
    }

    public Slider(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSliderView(context, false);
    }

    public void setSliderBackground(Drawable background) {
        this.mBackground = background;
        invalidate();
    }

    public void setPositionListener(SliderPositionListener listener) {
        this.mListener = listener;
    }

    public void setmax(double max) {
        this.positionmax = max;
    }

    public void setPosition(double position) {
        if (this.mPosition != position) {
            invalidate();
            this.mPosition = position;
            if (this.mListener != null) {
                this.mListener.onPositionChange(this.mPosition * this.positionmax);
            }
        }
    }

    public void setin_pos(double position) {
        double temp = position / this.positionmax;
        if (this.mPosition != temp) {
            this.mPosition = temp;
            invalidate();
        }
        if (this.mListener != null) {
            this.mListener.disPosition(this.mPosition * this.positionmax);
        }
    }

    protected void initSliderView(Context context, boolean vertical) {
        this.mPosition = 0.0d;
        this.mVertical = vertical;
        Resources res = context.getResources();
        if (this.mVertical) {
            this.mBackground = res.getDrawable(R.color.white_);
        } else {
            this.mBackground = res.getDrawable(R.color.butc);
        }
        this.mIndicator = res.getDrawable(R.drawable.scrubber_control_holo_dark);
//        if (this.mVertical) {
//            this.mBackground = res.getDrawable(R.drawable.scrubber_vertical_blue_holo_dark);
//        } else {
//            this.mBackground = res.getDrawable(R.drawable.scrubber_horizontal_holo_dark);
//        }
        setOnTouchListener(this.mClickListener);
    }

    protected void onDraw(Canvas canvas) {
        Rect r = new Rect();
        getDrawingRect(r);
        if (this.mVertical) {
            int lineX = r.centerX();
            int bgW = this.mBackground.getIntrinsicWidth() / 2;
            if (bgW == 0) {
                bgW = 5;
            }
            this.mBackground.setBounds(lineX - bgW, r.top + 10, lineX + bgW, r.bottom - 10);
            this.mBackground.draw(canvas);
            Utilities.centerAround(lineX, ((int) (((double) r.bottom) - (((double) (r.height() - 48)) * this.mPosition))) - 24, this.mIndicator);
            this.mIndicator.draw(canvas);
            return;
        }
        int lineY = r.centerY();
        int bgH = this.mBackground.getIntrinsicHeight() / 2;
        if (bgH == 0) {
            bgH = 5;
        }
        this.mBackground.setBounds(r.left + 10, lineY - bgH, r.right - 10, lineY + bgH);
        this.mBackground.draw(canvas);
        Utilities.centerAround((((int) (((double) (r.width() - 48)) * this.mPosition)) + r.left) + 24, lineY, this.mIndicator);
        this.mIndicator.draw(canvas);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (this.mVertical) {
            setMeasuredDimension(65, getMeasuredHeight());
        } else {
            setMeasuredDimension(getMeasuredWidth(), this.mIndicator.getIntrinsicHeight());
        }
    }
}
